﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml.Linq;
using Interfleet.SE.UICompiler.Core;
using Interfleet.SE.UICompiler.Core.Batch;
using Moq;
using Interfleet.SE.UICompiler.Core.ClosureCompiler;

namespace Interfleet.SE.UICompiler.Tests.Batch
{
    /// <summary>
    /// Summary description for BatchTests
    /// </summary>
    [TestClass]
    public class BatchJobTests
    {
        public BatchJobTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        private XElement GetTestXml(CompileSupplier compileSupplier, string outputFile, List<string> files)
        {
            var sb = new StringBuilder();
            sb.AppendFormat("<Batch compileSupplier=\"{0}\" compileOption=\"SIMPLE_OPTIMIZATIONS\" outputFile=\"{1}\">", compileSupplier.ToString(), outputFile);
            sb.Append("<Files>");
            foreach (var file in files)
            {
                sb.AppendFormat("<File>{0}</File>", file);
            }
            sb.Append("</Files>");
            sb.Append("</Batch>");
            return XElement.Parse(sb.ToString());
        }

        private XElement GetTestXml(CompileSupplier compileSupplier)
        {
            var files = new List<string> { @"c:\test.js", @"c:\test2.js" };
            return GetTestXml(compileSupplier, @"c:\output.js", files);
        }

        private XElement GetTestXml()
        {
            return GetTestXml(CompileSupplier.ClosureCompilerLocal);
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void BatchJob_Constructor_WhenFileNameIsEmpty_Then_ThrowArgumentNullException()
        {
            var batch = new BatchJob(null);
            Assert.Fail();
        }

        [TestMethod]
        public void BatchJob_VerifyCompileType()
        {
            var mock = new Mock<IFileProvider>();
            string xml = GetTestXml().ToString();
            mock.Setup(f => f.ReadFileToEnd(It.IsAny<string>())).Returns(xml);
            var batch = new BatchJob("file", mock.Object);
            Assert.AreEqual(CompileType.ClosureCompilerLocal.Supplier, batch.CompileType.Supplier);
        }

        [TestMethod]
        public void BatchJob_VerifyCompileOption()
        {
            var mock = new Mock<IFileProvider>();
            string xml = GetTestXml().ToString();
            mock.Setup(f => f.ReadFileToEnd(It.IsAny<string>())).Returns(xml);
            var batch = new BatchJob("file", mock.Object);
            Assert.AreEqual("SIMPLE_OPTIMIZATIONS", batch.CompileOption);
        }


        [TestMethod]
        public void BatchJob_Verify_Files()
        {
            var mock = new Mock<IFileProvider>();
            string xml = GetTestXml().ToString();
            mock.Setup(f => f.ReadFileToEnd(It.IsAny<string>())).Returns(xml);
            var files = new List<string> { @"c:\test.js", @"c:\test2.js" };
            var batch = new BatchJob("file", mock.Object);
            CollectionAssert.AreEquivalent(files, new List<string>(batch.Files));
        }

        [TestMethod]
        public void BatchJob_Verify_OutputFile()
        {
            var mock = new Mock<IFileProvider>();
            string xml = GetTestXml().ToString();
            mock.Setup(f => f.ReadFileToEnd(It.IsAny<string>())).Returns(xml);
            var batch = new BatchJob("file", mock.Object);
            Assert.AreEqual(@"c:\output.js", batch.OutputFile);
        }

        [TestMethod]
        public void BatchJob_DecorateArguments_VerifyCompileOption()
        {
            var mock = new Mock<IFileProvider>();
            string xml = GetTestXml().ToString();
            mock.Setup(f => f.ReadFileToEnd(It.IsAny<string>())).Returns(xml);
            var batch = new BatchJob("file", mock.Object);
            Arguments arguments = new CCArguments();
            batch.DecorateArguments(arguments);
            Assert.AreEqual("SIMPLE_OPTIMIZATIONS", arguments.CompileOption);
        }

        [TestMethod]
        public void BatchJob_DecorateArguments_VerifyOutputFile()
        {
            var mock = new Mock<IFileProvider>();
            string xml = GetTestXml().ToString();
            mock.Setup(f => f.ReadFileToEnd(It.IsAny<string>())).Returns(xml);
            var batch = new BatchJob("file", mock.Object);
            Arguments arguments = new CCArguments();
            batch.DecorateArguments(arguments);
            Assert.AreEqual(@"c:\output.js", arguments.CompiledFileName);
        }

        [TestMethod]
        public void BatchJob_DecorateArguments_VerifyFilesCountIs2()
        {
            var mock = new Mock<IFileProvider>();
            string xml = GetTestXml().ToString();
            mock.Setup(f => f.ReadFileToEnd(It.IsAny<string>())).Returns(xml);
            var batch = new BatchJob("file", mock.Object);
            Arguments arguments = new CCArguments();
            batch.DecorateArguments(arguments);
            Assert.AreEqual(2, arguments.Files.Count);
        }
    }
}
